package com.example.lastwork;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class register extends Activity implements View.OnClickListener{

    private String realCode;
    private Button mBtRegisteractivityRegister;
    private EditText mEtRegisteractivityUsername;
    private EditText mEtRegisteractivityname;
    private EditText mEtRegisteractivityPassword1;
    private EditText mEtRegisteractivityPassword2;
    private EditText mEtRegisteractivityPhonecodes;
    private ImageView mIvRegisteractivityShowcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        initView();

        UserDao userDao = new UserDao();

        //将验证码用图片的形式显示出来

        mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
        realCode = Code.getInstance().getCode().toLowerCase();
    }

    private void initView(){
        mBtRegisteractivityRegister = findViewById(R.id.button_register);
        mEtRegisteractivityUsername = findViewById(R.id.E3);
        mEtRegisteractivityname = findViewById(R.id.E5);
        mEtRegisteractivityPassword1 = findViewById(R.id.E4);
        mEtRegisteractivityPassword2 = findViewById(R.id.E6);
        mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes);
        mIvRegisteractivityShowcode = findViewById(R.id.showCode);

        /**
         * 注册页面能点击的就三个地方
         * top处返回箭头、刷新验证码图片、注册按钮
         */

        mIvRegisteractivityShowcode.setOnClickListener(this);
        mBtRegisteractivityRegister.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.showCode:    //改变随机验证码的生成
                mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                break;
            case R.id.button_register:    //注册按钮
                //获取用户输入的用户名、密码、验证码
                String id = mEtRegisteractivityUsername.getText().toString().trim();
                String username = mEtRegisteractivityname.getText().toString().trim();
                String password1 = mEtRegisteractivityPassword1.getText().toString().trim();
                String password2 = mEtRegisteractivityPassword2.getText().toString().trim();
                String phoneCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase();
                //注册验证
                if (!TextUtils.isEmpty(id) &&!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password1)&& !TextUtils.isEmpty(password2) && !TextUtils.isEmpty(phoneCode) ) {
                    if (phoneCode.equals(realCode)) {
                        if (!(password1==password2)) {
                            String what = "password";
                            String a = null;
                            try {
                                a = UserDao.select(id,what);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if(!TextUtils.isEmpty(a)) {
                                try {
                                    UserDao.add(id, password2, username);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                Toast.makeText(this, "验证通过，注册成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(this, login.class);
                                startActivity(intent);
                                finish();
                            }else {
                                Intent intent2 = new Intent(this, register.class);
                                startActivity(intent2);
                                finish();
                                Toast.makeText(this, "用户已存在，请重新输入", Toast.LENGTH_SHORT).show();
                            }
                        }else
                        {
                            Toast.makeText(this, "密码输入不一致,请重新输入", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "验证码错误,注册失败", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "未完善信息，注册失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }
}
